'use strict'
var GameState = require("gameState");
var gameManager = require("gameManager");

/**
 * 玩家
 * 障碍基础类
 */

cc.Class({
  extends: cc.Component,

  properties: {

    playerSpriteAtlas: {
      default: null,
      type: cc.SpriteAtlas
    },

    leftSprite: {
      default: null,
      type: cc.Sprite
    },

    middleSprite: {
      default: null,
      type: cc.Sprite
    },

    rightSprite: {
      default: null,
      type: cc.Sprite
    },

    readHeartNode: {
      default: null,
      type: cc.Node
    },

    leftAnimation: {
      default: null,
      type: cc.Animation
    },

    middleAnimation: {
      default: null,
      type: cc.Animation
    },

    rightAnimation: {
      default: null,
      type: cc.Animation
    },

    baozhaAnimation: {
      default: null,
      type: cc.Animation
    },

    roadblockBoxCollider: {
      default: null,
      type: cc.BoxCollider
    },

    ZhuangFeiClip: {
      url: cc.AudioClip,
      default: null
    },

    ZhuangJiClip: {
      url: cc.AudioClip,
      default: null
    },

    // nameLabel: {
    //   url: cc.Label,
    //   default: null
    // },
  },

  // use this for initialization
  onLoad: function () {
    var screen = cc.view.getVisibleSize();
    this.screenWidth = screen.width;
    this.screenHeight = screen.height;
    this.state = GameState.STOP;
    this.score = 0;
    // this.baozhaAnimation = this.baozhaNode.getComponent(cc.Animation);
    this.leftAnimation.on('finished', this.onBaoZhaFinished, this);
    this.peopleNum = 0;
    this.needDel = false;
  },

  // called every frame
  update: function (dt) {
    if (this.state == GameState.RUNNING) {
      this.node.y -= this.speed * dt;
      if(this.node.y < -600)
      {
        if(this.needDel)
        {
          cc.log("---> 14 update remove : " + this.node.name);
          this.stop();
          this.roadblockManager.removeRoadblock(this.node);
          this.needDel = false;
          return;
        }
        this.needDel = true;
      }
    }
  },

  onCollisionEnter: function (other, self) {
    // cc.log(this.node.name + "----roadblock onCollisionEnter: " + other.node.name);
    if (other.node.name != "playerManager") {
      return;
    }

    if(this.state != GameState.RUNNING){
      return;
    }

    if (this.readHeartNode != null) {
      this.readHeartNode.active = false;
    }

    cc.log("---> 11 onCollisionEnter name : " + other.node.name + " && " + self.node.name);
    this.stop();

    //计算分数
    gameManager.instance.addScore(this.score, this.magicScore, this.isBlock);

    //撞击效果，暂时没用
    if (this.ZhuangJiClip != null) {
      var id = cc.audioEngine.play(this.ZhuangJiClip, false, 1);
    }

    //碰撞展现效果
    this.baozhaAnimation.node.active = true;
    this.baozhaAnimation.play();

    if (this.isBlock && !gameManager.instance.isSuperState()) {
      //this.node.destroy();
      return;
    }

    if (this.ZhuangFeiClip != null) {
      var id = cc.audioEngine.play(this.ZhuangFeiClip, false, 1);
    }

    if (this.leftAnimation != null) { this.leftAnimation.play(); }
    if (this.middleAnimation != null) { this.middleAnimation.play(); }
    if (this.rightAnimation != null) { this.rightAnimation.play(); }

  },

  onBaoZhaFinished: function () {
    // cc.log(this.node.name + ' onBaoZhaFinished');
    // this.node.destroy();
    cc.log("---> 12 onBaoZhaFinished remove : " + this.node.name);
    this.roadblockManager.removeRoadblock(this.node);
  },

  //更换图片资源，左边、右边人物图片
  initRoadblock: function (roadblockManager, leftSpriteName, middleSpriteName, rightSpriteName, isBlock, peopleNum) {
    this.roadblockManager = roadblockManager;
    this.isBlock = isBlock
    this.peopleNum = peopleNum
    cc.log("----> " + leftSpriteName + "----> " + middleSpriteName + "----> " + rightSpriteName)
    if (this.leftSprite != null) {
      this.leftSprite.spriteFrame = this.playerSpriteAtlas.getSpriteFrame(leftSpriteName);
    }
    if (this.middleSprite != null) {
      this.middleSprite.spriteFrame = this.playerSpriteAtlas.getSpriteFrame(middleSpriteName);
    }
    if (this.rightSprite != null) {
      this.rightSprite.spriteFrame = this.playerSpriteAtlas.getSpriteFrame(rightSpriteName);
    }

  },
  
  //重新设置基本数据，score 为空则表示玩家死亡
  resetRoadblockData: function (score, magicScore) {
    this.needDel = false;
    this.score = score
    this.magicScore = magicScore
    if (this.leftSprite != null) {
      var node = this.leftSprite.node;
      node.setPosition(cc.p(0, 0));
      node.rotation = 0;
      node.opacity = 255;
      node.active = true;
    }
    if (this.middleSprite != null) {
      var node = this.middleSprite.node;
      node.setPosition(cc.p(0, 0));
      node.rotation = 0;
      node.opacity = 255;
      node.active = true;
    }
    if (this.rightSprite != null) {
      var node = this.rightSprite.node;
      node.setPosition(cc.p(0, 0));
      node.rotation = 0;
      node.opacity = 255;
      node.active = true;
    }

    if (this.readHeartNode != null) {
      this.readHeartNode.active = !this.isBlock;
    }

    if (this.leftAnimation != null) { this.leftAnimation.resume(); this.leftAnimation.stop(); }
    if (this.middleAnimation != null) { this.middleAnimation.resume(); this.middleAnimation.stop(); }
    if (this.rightAnimation != null) { this.rightAnimation.resume(); this.rightAnimation.stop(); }
  },

  hide: function () {
    cc.log("---->roadblock: " + this.node.name + " hide " );
    this.node.active = false;
    this.roadblockBoxCollider.enabled = false;
  },

  show: function() {
    cc.log("---->roadblock: " + this.node.name + " show " );
    this.node.active = true;
    this.roadblockBoxCollider.enabled = true;
  },

  // 开始
  running: function (speed) {
    this.state = GameState.RUNNING;
    this.setSpeed(speed);
  },

  // 暂停
  pause: function () {
    this.state = GameState.PAUSE;
  },

  // 恢复
  continue: function () {
    this.state = GameState.RUNNING;
  },

  // 停止
  stop: function () {
    this.state = GameState.STOP;
  },

  // 加速
  addSpeed: function (value) {
    this.speed += value;
  },

  // 减速
  substractSpeed: function (value) {
    this.speed -= value;
    if (this.speed < 0) {
      this.speed = 0;
    }
  },

  // 设置速度
  setSpeed: function (value) {
    cc.log("roadblock: " + this.node.name + " setSpeed : " + value);
    this.speed = value;
  },

  getpeopleNum: function () {
    return this.peopleNum;
  },

  isRoadBlock: function () {
    return this.isBlock;
  }


});

